// import axios from 'axios';
import ajax, { toOptions } from './ajax';
import type { RouteRecordNormalized } from 'vue-router';
import { UserState } from '@/store/modules/user/types';
import { getUserId } from '@/utils/auth';
import appClientMenus from '@/router/app-menus';

export interface LoginData {
    applicationId: number;
    userCode: string;
    password: string;
    rememberMe: boolean;
}

export interface LoginRes {
    token: string;
    userId: number;
    userName: string;
    applicationId: number;
    tenantId: number;
}
export async function login(data: LoginData) {
    //let res = await ajax.post<LoginRes>('/ControlPanels/Authenticate', data);
    let res = await ajax.post<LoginRes>('/Panels/Authenticate', data);
    userInfo = null;
    console.log('login', res);
    return res;
}

let userInfo: any;
export async function getUserInfo() {
    const userId = getUserId();
    if (!userInfo) {
        userInfo = await ajax.get<UserState>(`/Panels/Users/${userId}`);
    }
    return userInfo;
}

export async function getMenuList() {
    const userId = getUserId();
    let menus = await ajax.get<Array<any>>(
        `/Panels/Users/${userId}/Menus`
    );

    //console.log("menus: ", menus);
    menus.sort((a: any, b: any) => a.menuIndex - b.menuIndex);
    //console.log("sorted menus: ", menus);
    let arr = menus
        .filter((a: any) => !a.parentId)
        .map((a: any) => {
            let children = menus
                .filter((b: any) => b.parentId === a.menuId)
                .map((b: any) => {
                    return {
                        path: b.pageUrl,
                        name: getMenuName(b.pageUrl), //b.menuCode,
                        meta: {
                            locale: b.menuName,
                            requiresAuth: true,
                            roles: ['*'],
                            hideInMenu: !b.visible,
                        },
                    };
                });
            return {
                path: a.pageUrl,
                name: getMenuName(a.pageUrl),
                meta: {
                    locale: a.menuName,
                    requiresAuth: true,
                    roles: ['*'],
                    hideInMenu: !a.visible,
                    icon: a.icon || 'icon-apps',
                    order: a.menuIndex,
                },
                children,
            };
        });
    //console.log("arr: ", arr);
    return arr;
}

// 用客户端代码中的name，否则找不到页面
function getMenuName(pageUrl: string) {
    let name = '';
    appClientMenus.forEach((a: any) => {
        if (a.path === pageUrl) {
            name = a.name;
        }
        a.children.forEach((b: any) => {
            if (`${a.path}/${b.path}` === pageUrl) {
                name = b.name;
            }
        });
    });
    return name;
}

export function getUser(code: string) {
    return ajax.get<LoginRes>(
        `/Panels/Applications/110/Users/Code/${encodeURIComponent(code)}`
    );
}

export async function ChangePassword(data: any) {
    return ajax.put<any>('/Panels/Users/Actions/Change/Password', data);
}

export function getRoles() {
    const userId = getUserId();
    return ajax.get<Array<any>>(`/Panels/Users/${userId}/Roles`);
}
